(function($, doc) {
	$.init();
	var lauchFlag = localStorage.getItem('lauchFlag');
	if(!lauchFlag) {
		$.openWindow({
			url: '../../guide.html',
			id: 'guide',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}
	var state = app.getState();
	if(state.user_id) {
		location.href = '../../main.html';
	}
	var accountBox = doc.getElementById('account');
	var loginButton = doc.getElementById('login');
	var pwdBox = doc.getElementById('password');
	var smsBox = doc.getElementById('code');
	var lastUser = app.getOneUser();
	if(lastUser && lastUser.user_id) {
		//accountBox.value = lastUser.phone;
	}

	function checkPhone(phone) {
		//		if(phone == '' || !(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
		//			plus.nativeUI.toast("手机号码有误，请重填手机号");
		//			return false;
		//		}
		if(phone == '') {
			plus.nativeUI.toast("手机号码 不能为空");
			return false;
		}
		return true;
	}

	function canLogin() {
		if(accountBox.value !== '' && pwdBox.value !== '') {
			loginButton.removeAttribute('disabled');
		} else {
			loginButton.setAttribute('disabled', 'disabled');
		}
	}
	accountBox.addEventListener('input', canLogin);
	pwdBox.addEventListener('input', canLogin);
	//smsBox.addEventListener('input', canLogin);

	mui('.mui-input-row').on('tap', '.mui-icon.mui-icon-clear', function() {
		loginButton.setAttribute('disabled', 'disabled');
	});

	loginButton.addEventListener('tap', function(event) {
		var loginInfo = {
			account: accountBox.value,
			password: pwdBox.value,
			//code: smsBox.value,
			//smstype: 1,
			data: 'c3203996977564eb33ac88a9eaedcddb', // 版本标记
			login_type: 1
		};

		if(!checkPhone(loginInfo.account)) {
			return;
		}
		if(loginInfo.password == '' || loginInfo.password.length < 6) {
			plus.nativeUI.toast('密码最短为6个字符');
			return;
		}
		/*
		if (!/^\d{6}$/.test(smsBox.value)) {
			plus.nativeUI.toast('验证码为6位数字');
			return;
		}*/

		loginInfo.passwd = md5(loginInfo.password);
		ajaxlogin(loginInfo, '../../main.html');
	});
	doc.querySelector('.link-area').addEventListener('tap', function() {
		//location.href = '../../reg.html'; 
		mui.openWindow('../../reg.html', 'reg');
	});
	doc.querySelector('.wjpass').addEventListener('tap', function() {
		//location.href = '../../reg.html'; 
		mui.openWindow('../../wjpass.html', 'wjpass');
	});
	var getCodeBox = document.getElementById('getCode');
	var c1 = 60;
	getCodeBox && getCodeBox.addEventListener('tap', function() {
		//console.log(appurl + 'chatapi/getsms');
		mui.post(appurl + 'ChatApi/getsms', {
			u: app.getState().user_id,
			smstype: 1
		}, function(res) {
			//console.log(JSON.stringify(res));
			if(res.status == 1) {
				mui.toast('发送短信成功');
			} else {
				mui.toast(res.msg);
				getCodeBox.innerHTML = '获取验证码';
				getCodeBox.removeAttribute('disabled');
				c1 = 60;
				return;
			}
			var code1 = setInterval(function() {
				getCodeBox.innerHTML = c1 + '秒后重新获取';
				c1 = c1 - 1;
				if(c1 >= 0) {
					getCodeBox.setAttribute("disabled", "disabled");
				} else {
					getCodeBox.innerHTML = '获取验证码';
					getCodeBox.removeAttribute('disabled');
					c1 = 60;
					clearInterval(code1);
				}
			}, 1000);
		});
	})
	/*
	doc.querySelector('#getCode').addEventListener('tap', function () {
		
		var name = accountBox.value;
		
		if (!name) {
			plus.nativeUI.toast('请先输入帐号！');
			return;
		}

		mui.get(apiurl + 'index/getsms', {
			user_name: name
		}, function(res) {
			mui.toast(res.msg);
		});
	});*/

	//	doc.getElementById('.wjpass').addEventListener('tap', function() {
	//			location.href = 'wjpass.html';
	//		}, false);

}(mui, document));